<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  confirm-text="完成"
  title="快速签收"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  size="small"

)
  mt-input(
    placeholder="请扫码条码号"
  )
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import { useNotify } from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
import { ESpecimenStatus } from "@scmt/api-common";
//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();
const editingObj = ref<{} | Object>({});

const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { data, request, loadingMap } = useRequest({
  save: (q) => {
    return $api.lis.specimen.batchGenerateBarcode(q).then(() => {
      $notify.success("作废成功");
      emit("message-to-parent");
      appmodel.value = false;
    });
  },
  listAsOptions: () => {
    return $api.lis.specimen.category.listAsOptions();
  },
  tubelistAsOptions: () => {
    return $api.lis.tube.listAsOptions();
  },
});

const updateConfirmModel = () => {
  if (!editingObj?.value?.reason) {
    $notify.error("请选择作废原因");
    return;
  }
  const specimenIdData = [].concat(props.selectionRow).map((item) => {
    return item.id;
  });
  request("save", {
    specimenList: specimenIdData,
    status: ESpecimenStatus.Rejection,
  });
};

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      request("listAsOptions");
      request("tubelistAsOptions");
      editingObj.value = {};
    }
  },
  {
    immediate: true,
  }
);
</script>
